package com.jhmk.jhecis.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.jhmk.jhecis.components.JsEngine;
import com.jhmk.jhecis.domain.DeptDict;
import com.jhmk.jhecis.domain.common.PageVm;
import com.jhmk.jhecis.domain.common.R;
import com.jhmk.jhecis.dto.ExecParm;
import com.jhmk.jhecis.domain.vo.ScriptContent;
import com.jhmk.jhecis.domain.InterfaceTool;
import com.jhmk.jhecis.service.InterfaceToolService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * JS接口工具对接信息存储表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-09-18
 */
@RestController
@RequestMapping("/interfaceTool")
@Tag(name = "接口工具接口",description = "接口工具接口")
@RequiredArgsConstructor
public class InterfaceToolController {

    final InterfaceToolService service;
    final JsEngine jsEngine;

    @GetMapping("getById")
    @Operation(description = "通过主键获取")
    public InterfaceTool getById(String id) {
        return service.getById(id);
    }

    @GetMapping("getList")
    @Operation(description = "查询所有列表")
    public List<InterfaceTool> getList() {
        QueryWrapper<InterfaceTool> queryWrapper = new QueryWrapper<>();
        return service.getBaseMapper().selectList(queryWrapper);
    }
    @PostMapping("getPageList")
    @Operation(description = "查询分页列表")
    public R<IPage<InterfaceTool>> getPageList(@RequestBody PageVm<String> req) {
        Page<InterfaceTool> page=new Page<>(req.getPageNo(),req.getPageSize());
        QueryWrapper<InterfaceTool> queryWrapper = new QueryWrapper<>();
        //通过查询条件
        queryWrapper.lambda().like(StrUtil.isNotEmpty(req.getSearch()) ,InterfaceTool::getInterfacename,req.getSearch());
        IPage<InterfaceTool> res=  service.page(page,queryWrapper);
        return R.ok(res);
    }

    @Operation(description = "通过编码获取指定Js接口")
    @GetMapping("/getByCode")
    public InterfaceTool GetByCode(String interfaceCode) {
        QueryWrapper<InterfaceTool> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(InterfaceTool::getInterfacecode,interfaceCode);
        return service.getOne(queryWrapper);
    }

    @Operation(description = "保存多条接口工具")
    @PostMapping("/saveBatch")
    public R<Boolean> saveBatch(@RequestBody List<InterfaceTool> interfaceToolList) {
        return R.ok(service.saveBatch(interfaceToolList));
    }

    @Operation(description = "保存多条接口工具")
    @PostMapping("/saveOne")
    public R<Boolean> saveOne(@RequestBody InterfaceTool interfaceToolList) {
        return R.ok(service.save(interfaceToolList));
    }

    @Operation(description = "保存除大字段外接口工具")
    @PostMapping("/saveInterfaceToolNoClob")
    public R<Boolean> saveInterfaceToolNoClob(@RequestBody List<InterfaceTool> interfaceToolList) {
        //return service.saveInterfaceToolNoClob(interfaceToolList);
       return R.ok();
    }

    @Operation(description = "执行JS脚本-数据库中取最新")
    @PostMapping("/execJSCode")
    public R<String> execJSCode(@RequestBody ScriptContent scriptContent) {
        return  jsEngine.execJs(scriptContent);
    }

    @Operation(description = "测试get请求js脚本")
    @PostMapping("/testJS")
    public R<String> testJS() {
        String code="function test7(parm){\n" +
                "    var head=[{\"Name\":\"auth\",\"Value\":\"jhecis\"}];\n" +
                "    var headstr= JSON.stringify(head);\n" +
                "    JSRegister.myLog(\"调用急诊接口入参：\"+parm);\n" +
                "    var res= JSRegister.RestSharpGet('http://192.168.103.161:8890','/api/DictHospital/GetDictHospital',headstr);\n" +
                "    return res;\n" +
                "}\n";
        ScriptContent scriptContent=new ScriptContent();
        scriptContent.setData("{\n" +
                "  \"javaScriptCode\": \"test7\",\n" +
                "  \"data\": \"我是参数\"\n" +
                "}");
        scriptContent.setJavaScriptCode(code);
        scriptContent.setFunctionName("test7");
        return jsEngine.execCacheJs(scriptContent);
    }
}
